package com.system.controller;

import com.system.dto.ReqConsumer;
import com.system.entity.Consumer;
import com.system.service.IConsumerService;
import com.system.util.QiniuUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
public class ConsumerController {

    @Resource
    private IConsumerService consumerService;



    @Autowired
    QiniuUtils qiniuUtils;

    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("/getAllConsumer")
    public List<Consumer> findAllConsumer(){
        return consumerService.findAll();
    }

    @GetMapping("/findByCid/{cid}")
    public Consumer findByCid(@PathVariable("cid") Integer cid){
        return consumerService.findByCid(cid);
    }

    @PostMapping("/consumerLogin")
    @ApiOperation(value = "消费者登录")
    public String consumerLogin(@RequestBody Map consumer) {
        String cname = (String) consumer.get("cname");
        String cpass = (String) consumer.get("cpass");

        if (cname != null && cpass != null) {
            if (consumerService.login(cname, cpass) != null) {
                System.out.println("ssdfsdsdds");
                return "1";
            }
        }
        return "0";
    }

    @PostMapping("/consumerRegistry")
    public String consumerRegistry(@RequestBody ReqConsumer reqConsumer) {
        System.out.println(reqConsumer);

        String email = reqConsumer.getEmail();
        String o = (String) redisTemplate.opsForValue().get(email);
        System.out.println(o);
        String code = reqConsumer.getCode();
        Consumer c= new Consumer();

        if (reqConsumer.getCname() != null && reqConsumer.getCpass() != null && o.equals(code)) {
            c.setCid(reqConsumer.getCid());
            c.setCname(reqConsumer.getCname());
            c.setCpass(reqConsumer.getCpass());
            c.setEmail(reqConsumer.getEmail());
            c.setCaddress(reqConsumer.getCaddress());
            c.setCsex(reqConsumer.getCsex());
            c.setCtel(reqConsumer.getCtel());
            c.setCimage(reqConsumer.getCimage());

            consumerService.saveConsumer(c);
            return "1";
        }
        return "0";
    }

    @PostMapping("/updateConsumer")
    public String updateConsumer(@RequestBody Consumer consumer){

        if(consumerService.updateConsumer(consumer)>0){
            return "1";
        }
        return "0";

    }

    @GetMapping("/sendMail")
    public String sendMail(@RequestParam("email") String email) {
        if(consumerService.sendMail(email)>0){
            return "1";
        }
        return "0";
    }

    @GetMapping("/findByEmail/{email}")
    public String findByEmail(@RequestParam("email") String email){
        if(consumerService.findByEmail(email)!= null){
            return "1";
        }
            return "0";
    }


    @PostMapping(value = "/consumerUpload")
    public String consumerUpload(@RequestParam("consumerFile") MultipartFile multipartFile) {

            String originalFilename = multipartFile.getOriginalFilename();
            System.out.println(originalFilename);
            return qiniuUtils.upload(multipartFile);
    }




}
